public with sharing class WBUtil {
    
    public static string AppKey{get{Weibo__c cs=Weibo__c.getInstance('AppKey');return cs.App_Key__c;}set;}
    public static string ClientSecret{get{Weibo__c cs=Weibo__c.getInstance('AppKey');return cs.client_secret__c;}set;}
    public static string Code{get{return WBSfdcInfo.instance().code;}set;}
    public static string Token{get{return WBSfdcInfo.instance().token;}set;}
    
    public static boolean isNvl(string s){
        return (s == null || s == '');
    }
    
    public static string sendRequest(string url,string Method){
			return sendRequest(url,Method,'');
    }
    
    public static string sendRequest(string url){
        return sendRequest(url,'POST','');
    }
    public static string sendRequest(string url,string Method,string body){
      string respVal = '';
    	try{
        // Create HTTP request to send.
        Http httpProtocol = new Http();
        HttpRequest req = new HttpRequest();
        req.setTimeout(10000);
        // Set the endpoint URL.
        req.setEndPoint(url);
        // Set the HTTP verb to POST.
        req.setMethod(Method);
        // Set request body
        if(!isNvl(body)){
        	req.setBody(body);
       		System.debug('@@@@HttpResponse body' + body);
        }
        // Send the HTTP request and get the response.
        HttpResponse response = makeCallout(req);
        
        // The response is in JSON format.
        respVal = response.getBody();
        System.debug('@@@@HttpResponse' + respVal);
	    }
	    catch(Exception e){
	    	if( Apexpages.currentPage() != null ){
                 ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, e.getMessage()));
	    	}
    	}
    	return respVal;
    }
    
    
    // ------Test Web call ----
    private static HttpResponse makeCallout(HttpRequest req) {
        HttpResponse res;
        if (Test.isRunningTest()) {
            res = createMockResponse();
        } else {
            res = new Http().send(req);
        }
        return res;
    }
    
    // Create fake response for the test
    private static HttpResponse createMockResponse() {
        HttpResponse res = new HttpResponse();
        res.setStatus('OK'); // all are new methods
        res.setStatusCode(200);
        String jsonStr =
            '{"request":"/oauth2/access_token","error_code":10021,"error":"HTTP METHOD is not suported for this request!"}';
        res.setBody(jsonStr);
        return res;
    }
    // Test method for testing the callout
    static testmethod void testCalloutMethod() {
        string respVal = WBUtil.sendRequest('https://api.weibo.com/oauth2/authorize','key=val');
        // Verify fake results
        System.assert(respVal != '');
    }
    
    private static testMethod void testisNvl(){
        string s;
        system.assertEquals(true, isNvl(s));
    }
    
}